package proj.java.dao;

import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import proj.java.bean.Disciplina;
import proj.java.factory.ConnectionFactory;

public class AdministradorServico {
	
	static ConnectionFactory banco;
	private static AdministradorServico instance;

	public static AdministradorServico getInstance() {
		if (instance == null) {
			banco = new ConnectionFactory();
			instance = new AdministradorServico(banco);
		}
		return instance;
	}
	
	public Connection connection;

	public AdministradorServico(ConnectionFactory banco) {
		this.connection = (Connection) banco.getConnection();
	}

	public AdministradorServico() {
		this.connection = (Connection) banco.getConnection();
	}
	
	public void inserirDisciplina(Disciplina disciplina) {

		try {

			String sql = "INSERT INTO tb_disciplina (nomeDisciplina, matriculaProfessor, "
					+ "nomeProfessor, periodicidade, periodo, cargaHoraria) "
					+ "VALUES (?,?,?,?,?,?)";

			PreparedStatement stmt = (PreparedStatement) connection
					.prepareStatement(sql);

			stmt.setString(1, disciplina.getNome());
			stmt.setString(2, disciplina.getMatriculaProfessor());
			stmt.setString(3, disciplina.getNomeProfessor());
			stmt.setString(4, disciplina.getPeriodicidade());
			stmt.setString(5, disciplina.getPeriodo());
			stmt.setString(6, disciplina.getCargaHoraria());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException sqle) {
			throw new RuntimeException(sqle);
		}

	}
	
	public void alterarDisciplina(Disciplina disciplina) {

		try {
			String sql = "UPDATE tb_disciplina SET matriculaProfessor = ?, "
					+ "nomeProfessor = ?, periodicidade = ?, periodo = ?, "
					+ "cargaHoraria = ? WHERE nomeDisciplina = ?";

			PreparedStatement stmt = (PreparedStatement) connection
					.prepareStatement(sql);

			stmt.setString(1, disciplina.getMatriculaProfessor());
			stmt.setString(2, disciplina.getNomeProfessor());
			stmt.setString(3, disciplina.getPeriodicidade());
			stmt.setString(4, disciplina.getPeriodo());
			stmt.setString(5, disciplina.getCargaHoraria());
			stmt.setString(6, disciplina.getNome());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException sqle) {
			throw new RuntimeException(sqle);
		}

	}
	
	public void excluirDisciplina(Disciplina disciplina) {

		try {

			String sql = "DELETE FROM tb_item WHERE nomeDisciplina = ?";

			PreparedStatement stmt = (PreparedStatement) connection
					.prepareStatement(sql);

			stmt.setString(1, disciplina.getNome());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}

	}
}
